﻿using CustomCharacter;
using CustomCharacter.Ability;
using Exiled.API.Enums;
using Exiled.Events.EventArgs.Interfaces;
using Exiled.Events.EventArgs.Player;
using PlayerRoles;
using PlayerStatsSystem;
using Respawning;
using RgCore.Events;
using RgCore.GameData;
using System.Collections.Generic;
using System.Linq;
using CustomCharacter.Interfaces;
using UnityEngine;
using Player = Exiled.API.Features.Player;

namespace Scp073
{
    [RoleInfo("SCP-073", Side.ChaosInsurgency, RoleTypeId.ChaosRifleman, RoleTypeId.ChaosRifleman)]
    public class Scp073 : CustomCharacter.CustomCharacter, IScpNumber, ITeamSpawn
    {
        public Scp073(int id) : base(id)
        {
        }

        public override IEnumerable<Ability> AbilityRegister()
        {
            return new Ability[]
            {
                new AttributeAbility("Scp073_Attr_1", "受到攻击时，会给予一定反伤（具体数值请查看腾讯文档）",
    Attribute_1, false, new IExiledEvent[] { EventProcessor.OnHurting }, null, GetType())
            };
        }

        public string ScpNumber => "073";

        public override Color32 RoleColor => new Color32(0, 255, 0, 255);

        public SpawnableTeamType SpawnType => SpawnableTeamType.ChaosInsurgency;

        public byte MaxWaves => 0;

        public void OnTeamSpawn(List<Player> allPlayers)
        {
            List<Player> ava = allPlayers.Where(x => x.Role.Type == Attribute.OriginRole && !x.IsCustomCharacter() &&
 GameplayData.GameDatas.FirstOrDefault(y => y.Player == x).KeyBinded).ToList();
            if (ava.Count > 0)
            {
                Player tar = ava[UnityEngine.Random.Range(0, ava.Count)];
                SpawnPlayer(tar, GetType());
            }
        }

        public static AbilityOutput Attribute_1(Player player, ref AbilityInfo info, IExiledEvent e, AbilityTrigger trigger)
        {
            if (trigger != AbilityTrigger.Event || e == null)
                return new AbilityOutput(false);
            if (e is HurtingEventArgs ev)
            {
                if (ev.Player == player)
                {
                    if (ev.DamageHandler.Base is AttackerDamageHandler adh)
                    {
                        if (adh is FirearmDamageHandler)
                        {
                            float am = ev.Amount;
                            if (ev.Player.IsScp)
                                am = ev.Attacker.MaxHealth * 0.01f;
                            else
                                am *= 0.66f;
                            ev.Attacker.ReferenceHub.playerStats.DealDamage(new CustomReasonDamageHandler("反伤") { Damage = am });
                            return new AbilityOutput(true);
                        }
                        switch(ev.Attacker.Role.Type)
                        {
                            case RoleTypeId.Scp3114:
                            case RoleTypeId.Scp0492:
                            case RoleTypeId.Scp096:
                            case RoleTypeId.Scp939:
                                ev.Attacker.ReferenceHub.playerStats.DealDamage(new CustomReasonDamageHandler("反伤") { Damage = ev.Amount });
                                ev.Amount *= 0.3f;
                                return new AbilityOutput(true);
                            case RoleTypeId.Scp173:
                                ev.Attacker.ReferenceHub.playerStats.DealDamage(new CustomReasonDamageHandler("反伤") { Damage = 100f });
                                ev.Amount = 50f;
                                return new AbilityOutput(true);
                        }
                    }
                }
            }
            return new AbilityOutput(true);
        }
    }
}